package com.qianguo.sender;


import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eva.androidlibrary.icon.FruitIcon;
import com.eva.androidlibrary.utils.IconfontUtil;
import com.eva.androidlibrary.widget.tabhost.FragmentTabHostLayout;
import com.eva.androidlibrary.widget.tabhost.FragmentTabHostLayoutAdapter;
import com.eva.androidlibrary.widget.tabhost.TabHostLayout;
import com.qianguo.sender.base.BaseTitleActivity;
import com.qianguo.sender.tab.SenderCatchOrderFragment_;
import com.qianguo.sender.tab.SenderOrderFragment_;
import com.qianguo.sender.tab.SenderProfileFragment_;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 配送员首页
 *
 * @author eva
 */
@EActivity(R.layout.activity_sender_main)
public class SenderMainActivity extends BaseTitleActivity {

    private static final int TAB_CATCH_ORDER = 0;
    private static final int TAB_ORDER = 1;
    private static final int TAB_PROFILE = 2;

    private static final int[] TABS_NAME = new int[]{R.string.sender_tab_catch_order, R.string.sender_tab_order, R.string.sender_tab_profile};

    private static final FruitIcon[] TABS_ICON = new FruitIcon[]{FruitIcon.TAB_ORDER, FruitIcon.TAB_SHOP, FruitIcon.TAB_MY_CENTER};
    private static final FruitIcon[] TABS_ICON_SELECTED = new FruitIcon[]{FruitIcon.TAB_ORDER, FruitIcon.TAB_SHOP_SELECTED, FruitIcon.TAB_MY_CENTER_SELECTED};

    @ViewById(R.id.fragment_tab_host_layout)
    FragmentTabHostLayout mFragmentTabHostLayout;
    private TabHostAdapter mAdapter;

    public static void jumpToMain(Context context) {
        context.startActivity(new Intent(context, com.qianguo.sender.SenderMainActivity_.class));
    }

    @Override
    protected String getToolbarTitle() {
        return null;
    }

    @Override
    protected boolean hasNavigationIcon() {
        return false;
    }

    @Override
    protected void initializeUI() {
        mAdapter = new TabHostAdapter(R.id.fragment_container, getSupportFragmentManager());
        mFragmentTabHostLayout.setAdapter(mAdapter);
        mFragmentTabHostLayout.setTabSelected(TAB_CATCH_ORDER);
        mFragmentTabHostLayout.setOnTabHostItemSelectedListener(new TabHostLayout.OnTabHostSelectedChangedListener() {
            @Override
            public void onSelected(View view, int position) {
                setToolBarTitle(getString(TABS_NAME[position]));
            }
        });
    }

    private class TabHostAdapter extends FragmentTabHostLayoutAdapter {

        public TabHostAdapter(int containerId, FragmentManager fm) {
            super(containerId, fm);
        }

        @Override
        public Fragment getFragmentInstance(int position) {
            Fragment f = null;
            switch (position) {
                case TAB_CATCH_ORDER:
                    f = new SenderCatchOrderFragment_();
                    break;
                case TAB_ORDER:
                    f = new SenderOrderFragment_();
                    break;
                case TAB_PROFILE:
                    f = new SenderProfileFragment_();
                    break;
            }
            return f;
        }

        @Override
        public int getCount() {
            return TABS_NAME.length;
        }

        @Override
        public View getTab(int position, boolean isSelected, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(SenderMainActivity.this).inflate(R.layout.activity_sender_tab_business, null);
            }

            TextView icon = (TextView) convertView.findViewById(R.id.business_tab_icon);
            if (isSelected) {
                IconfontUtil.setIcon(SenderMainActivity.this, icon, TABS_ICON_SELECTED[position]);
            } else {
                IconfontUtil.setIcon(SenderMainActivity.this, icon, TABS_ICON[position]);
            }

            TextView name = (TextView) convertView.findViewById(R.id.business_tab_text);
            name.setText(getString(TABS_NAME[position]));
            return convertView;
        }
    }
}
